package com.loop.unclelend.ui.fragment

import android.view.LayoutInflater
import android.view.View
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPagePinPaymentProcessBinding
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.ui.activity.Act_Pin_Payment_Status
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import com.loop.unclelend.utils.thread.TimingThread
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/28 17:04
 * @Annotation：支付结果处理中
 */
class Fm_Pin_Payment_Process(): ProFragment(), TimingThread.ITimingThreadListener {
    lateinit var binding: UiPagePinPaymentProcessBinding
    private var orderData: JSONObject? = null
    var act: Act_Pin_Payment_Status? = null

    //倒计时
    private var countdown = 30
    private var timeThread: TimingThread? = null

    constructor(act: Act_Pin_Payment_Status,data: JSONObject): this(){
        this.act = act
        this.orderData = data
    }
    override fun getUncleRootView(inflater: LayoutInflater): View {
        binding = UiPagePinPaymentProcessBinding.inflate(inflater)
        return binding.root
    }

    override fun initUncleView() {
        setUncleTitleBarVisibility(View.VISIBLE)
        setUncleBackBtnRes(R.mipmap.icon_back)
        setUncleTextTitle(getString(R.string.loan_payment_status_title))
        setUncleRightBtnRes(R.mipmap.icon_kefu)
    }

    override fun initUncleListener() {
        //客服
        rootViewBinding.imgKefu.setOnClickListener {
            showUncleKefuDialog("PIN PaymentProcess Page")
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_CUSTOMER,"PIN PaymentProcess Page")
        }
        //刷新
        binding.layoutRefresh.setOnClickListener {
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LOAN_PAYMENT_PROCESS_PAYMENT_UPDATE,"TransactionId：${orderData!!.optString("transactionId")}")
            //再次调用查询状态接口
            act!!.presenter!!.requestUnclePinPaymentStatusAction()
        }

        //再次还款
        binding.btnPaymentAgain.setOnClickListener {
            UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LOAN_PAYMENT_PROCESS_PAYMENT_AGAIN,"TransactionId：${orderData!!.optString("transactionId")}")
            act!!.presenter!!.requestUncleLoanPayLinkAction()
        }
    }

    override fun initUncleData() {
        refreshData(this.orderData!!)
    }

    /**
     * 刷新数据
     */
    fun refreshData(data: JSONObject){
        this.orderData = data
        val realRepayAmount = data.optString("realRepayAmount","")
        binding.txtAmount.text = realRepayAmount
        binding.txtProduct.text = data.optString("product","")

        binding.txtProcessedReason.text = String.format(getString(R.string.loan_payment_status_processed_reason1),realRepayAmount)

        //是否开启倒计时
        if (data.optBoolean("isStartCountdown",true)){
            startTimeThread()
        }
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        countdown = 30
        showUnclePaymentStatus()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    /**
     * 倒计时
     */
    override fun onLoading() {
        if (countdown <= 0) {
            countdown = 30
            timeThread!!.stopThread()

            //倒计时结束，自动调一次状态查询接口
            act!!.presenter!!.requestUnclePinPaymentStatusAction(false)
            return
        }
        countdown--
        showUnclePaymentStatus()
    }

    /**
     * 设置按钮状态
     */
    private fun showUnclePaymentStatus(){
        if (countdown <= 0){
            binding.btnPaymentAgain.isEnabled = true
            binding.btnPaymentAgain.isClickable = true
            binding.btnPaymentAgain.text = String.format(getString(R.string.loan_payment_status_make_payment_again),"")
        } else {
            binding.btnPaymentAgain.isEnabled = false
            binding.btnPaymentAgain.isClickable = false
            binding.btnPaymentAgain.text = String.format(getString(R.string.loan_payment_status_make_payment_again),"(${countdown}s)")
        }
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_PIN_REPAYMENT_PROCESS_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_LOAN_PIN_REPAYMENT_PROCESS_END)
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}